#set security code
li $1, 2
# $10 is always 1
li $10, 1

$CheckCode$
#reset regs
li $11, 0
#display 0000 on the LED
sw $0, $0(259)
#check for pb1 button pressed
lw $3, $0(257)
beq $3, $0($CheckCode$)
#load code in the switches, --reuse $2
lw $2, $0(256)
# Branch if it is wrong
bne $1, $2($CheckCode$)

$CorrectCode$
#display 1111 on the LED
li $6, 15
sw $6, $0(259)

# $15 * $7 = 10 seconds
li $15,1000
$LoopA$
sub $15,$15,$10
li $7, 65535

$LoopB$
#decrement loop counter
sub $7, $7, $10
#go to CheckCode if time is out
beq $15, $0($CheckCode$)
#go to LoopA if $7 is equal to 0
beq $7, $0($LoopA$)
#go to LoopB if pb2 is not pressed
lw $9, $0(258)
beq $9, $0($LoopB$)

$Balance$
#increment number of coins
add $11, $11, $10
#display number of coins
sw $11, $0(259)
# $16 * $12 = 10 seconds
li $16,1000
$LoopC$
sub $16,$16,$10
li $12, 65535

$LoopD$
#decrement loop counter
sub $12, $12, $10
#go to CheckCode if time is out
beq $16, $0($CheckCode$)
#go to LoopC if $12 is equal to 0
beq $12, $0($LoopC$)
#go to LoopD if neither pb1 or pb2 was pressed --reuse $4, $5, $6 --
lw $13, $0(257)
lw $14, $0(258)
or $5, $13, $14
beq $5, $0($LoopD$)
#go to Balance if coin is added(pb2 was pressed) else (pb1 was pressed) --reuse $7, $8, $9 --
bne $14, $0($Balance$)
#check for enough coins
li $9, 3
slt $8, $11, $9
#go to LoopD if not enough money
bne $8, $0($LoopD$)

$Deliver$
#take away 3 coins
sub $11, $11, $9
#output new count to LEDs
sw $11, $0(259)
#$17 * $12 = 10 seconds
li $17,1000
$LoopE$
sub $17, $17, $10
li $12, 65535

$LoopF$
#decrement loop counter
sub $12, $12, $10
#go to CheckCode if time is out
beq $17, $0($CheckCode$)
#go to LoopE if $12 is equal to 0
beq $12, $0($LoopE$)
#Load pb2
lw $14, $0(258)
#go to LoopF if pb2 is not pressed
beq $14, $0($LoopF$)
#go to CorrectCode if pb2 is pressed
bne $14, $0($CorrectCode$)